<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        clearable
        @keyup.enter.native="handleFind"
        class="filter-item"
        v-model="keyword"
        placeholder="请输入类型或名称"
      ></el-input>

      <el-button type="primary" icon="el-icon-search" @click="handleFind">查找</el-button>
      <el-button type="success" @click="handleAddSysDictType" icon="el-icon-edit">添加</el-button>
    </div>
    <el-table :data="tableData" v-loading="Loading" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table
            border
            :data="scope.row.sysDictDataList"
            :show-header="showHeader"
            style="width: 100%"
          >
            <el-table-column label="序号" width="60" align="center">
              <template slot-scope="scope">
                <span>({{scope.$index + 1}})</span>
              </template>
            </el-table-column>

            <el-table-column label="字典标签" min-width="120" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.dictLabel }}</span>
              </template>
            </el-table-column>

            <el-table-column label="字典键值" min-width="120" align="center">
              <template slot-scope="scope">
                <el-tag v-if="scope.$index%5 == 0" type="warning" size="small" effect="plain">{{scope.row.dictValue}}</el-tag>
                <el-tag v-if="scope.$index%5 == 1" type="success" size="small" effect="plain">{{scope.row.dictValue}}</el-tag>
                <el-tag v-if="scope.$index%5 == 2" type="info" size="small" effect="plain">{{scope.row.dictValue}}</el-tag>
                <el-tag v-if="scope.$index%5 == 3" type="danger" size="small" effect="plain">{{scope.row.dictValue}}</el-tag>
                <el-tag v-if="scope.$index%5 == 4">{{scope.row.dictValue}}</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="是否默认" align="center">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.isDefault==1" size="small" effect="plain">是</el-tag>
                <el-tag type="warning" v-if="scope.row.isDefault==0" size="small" effect="plain">否</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="回显样式" align="center">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.listClass == 'default'">{{scope.row.listClass}}</el-tag>
                <el-tag
                  v-if="scope.row.listClass == 'success'"
                  type="success"
                >{{scope.row.listClass}}</el-tag>
                <el-tag
                  v-if="scope.row.listClass == 'primary'"
                  type="primary"
                >{{scope.row.listClass}}</el-tag>
                <el-tag v-if="scope.row.listClass == 'info'" type="info" size="small" effect="plain">{{scope.row.listClass}}</el-tag>
                <el-tag
                  v-if="scope.row.listClass == 'warning'"
                  type="warning"
                  size="small" effect="plain"
                >{{scope.row.listClass}}</el-tag>
                <el-tag v-if="scope.row.listClass == 'danger'" type="danger" size="small" effect="plain">{{scope.row.listClass}}</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="发布状态" align="center">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.isPublish == '1' " size="small" effect="plain">上架</el-tag>
                <el-tag type="danger" v-else size="small" effect="plain">下架</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="备注" min-width="200" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.remark }}</span>
              </template>
            </el-table-column>

            <el-table-column label="创建时间" min-width="160" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.createTime }}</span>
              </template>
            </el-table-column>

            <el-table-column label="更新时间" min-width="160" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.updateTime }}</span>
              </template>
            </el-table-column>

            <el-table-column label="状态" align="center">
              <template slot-scope="scope">
                <template v-if="scope.row.status == 1">
                  <span>正常</span>
                </template>
                <template v-if="scope.row.status == 2">
                  <span>推荐</span>
                </template>
                <template v-if="scope.row.status == 0">
                  <span>已删除</span>
                </template>
              </template>
            </el-table-column>

            <el-table-column label="操作" min-width="160" align="center">
              <template slot-scope="scope">
                <el-button
                  @click="handleStickSysDictData(scope.row)"
                  type="success"
                  size="small"
                  plain
                >置顶</el-button>
                <el-button
                  @click="handleEditSysDictData(scope.row)"
                  type="primary"
                  size="small"
                  plain
                >编辑</el-button>
                <el-button
                  @click="handleDeleteSysDictData(scope.row)"
                  type="danger"
                  size="small"
                  plain
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="字典名称" min-width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.dictName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="字典类型" min-width="160" align="center">
        <template slot-scope="scope">
          <el-tag type="info" style="cursor: pointer" size="small" effect="plain">{{ scope.row.dictType }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="备注" min-width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>

      <el-table-column label="发布状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isPublish == '1' " size="small" effect="plain">上架</el-tag>
          <el-tag type="danger" v-else size="small" effect="plain">下架</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" min-width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="更新时间" min-width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.status == 1">
            <span>正常</span>
          </template>
          <template v-if="scope.row.status == 2">
            <span>推荐</span>
          </template>
          <template v-if="scope.row.status == 0">
            <span>已删除</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="160">
        <template slot-scope="scope">
          <el-button @click="handleAddSysDictData(scope.row)" type="success" size="small">添加值</el-button>
          <el-button @click="handleEditSysDictType(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDeleteSysDictType(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 类型对话框 添加或修改对话框 -->
    <el-dialog
      :title="title"
      v-if="dialogTypeFormVisible"
      :visible.sync="dialogTypeFormVisible"
      :before-close="handleTypeDialogClose"
      :close-on-click-modal="false"
      width="600px"
    >
      <el-form :model="typeForm" :rules="rules" ref="typeForm">
        <el-form-item label="字典类型" :label-width="formLabelWidth" prop="dictType">
          <el-input v-model="typeForm.dictType" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="字典名称" :label-width="formLabelWidth" prop="dictName">
          <el-input v-model="typeForm.dictName" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="typeForm.remark" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="是否发布" :label-width="formLabelWidth" prop="isPublish">
          <el-radio-group v-model="typeForm.isPublish" size="small">
            <el-radio-button label="1" border>发布</el-radio-button>
            <el-radio-button label="0" border>下架</el-radio-button>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleTypeDialogClose">取 消</el-button>
        <el-button type="primary" @click="submitTypeForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 字典值对话框 添加或修改对话框 -->
    <el-dialog
      :title="title"
      v-if="dialogDataFormVisible"
      :visible.sync="dialogDataFormVisible"
      :before-close="handleDataDialogClose"
      :close-on-click-modal="false"
      width="600px"
    >
      <el-form :model="dataForm" :rules="rules" ref="dataForm">
        <el-form-item label="字典标签" :label-width="formLabelWidth" prop="dictLabel">
          <el-input v-model="dataForm.dictLabel" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="字典键值" :label-width="formLabelWidth" prop="dictValue">
          <el-input v-model="dataForm.dictValue" auto-complete="off"></el-input>
        </el-form-item>

        <!-- <el-form-item label="字典类型" :label-width="formLabelWidth" prop="dictTypeUid">
          <el-select
            disabled
            v-model="dataForm.dictTypeUid"
            placeholder="请选择字典类型"
            style="width:200px"
          >
            <el-option
              v-for="item in dictTypeList"
              :key="item.uid"
              :label="item.dictType"
              :value="item.uid"
            ></el-option>
          </el-select>
        </el-form-item>-->


        <el-form-item label="扩展样式" :label-width="formLabelWidth">
          <el-input v-model="dataForm.cssClass" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="dataForm.remark" auto-complete="off"></el-input>
        </el-form-item>
        
        <el-form-item label="回显样式" :label-width="formLabelWidth">
          <el-select v-model="dataForm.listClass" clearable placeholder="请选择" style="width:140px">
            <el-option
              v-for="item in listClassType"
              :key="item.key"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="系统默认" :label-width="formLabelWidth" prop="isDefault">
          <el-radio-group v-model="dataForm.isDefault" size="small">
            <el-radio-button label="1" border>是</el-radio-button>
            <el-radio-button label="0" border>否</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="发布状态" :label-width="formLabelWidth" prop="isPublish">
          <el-radio-group v-model="dataForm.isPublish" size="small">
            <el-radio-button label="1" border>发布</el-radio-button>
            <el-radio-button label="0" border>下架</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDataDialogClose">取 消</el-button>
        <el-button type="primary" @click="submitDataForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSysDictTypeList,
  addSysDictType,
  editSysDictType,
  deleteSysDictType
} from "@/api/sysDictType";
import { addSysDictData,editSysDictData,deleteSysDictData,stickSysDictData } from "@/api/sysDictData";
export default {
  data() {
    return {
      Loading: true,
      keyword: "",
      query: {}, // 查询对象
      tableData: [],
      currentPage: 1,
      pageSize: 15,
      total: 0, //总数量
      title: "增加字典类型",
      dialogTypeFormVisible: false, //字典类型，控制弹出框
      dialogDataFormVisible: false, //字典值，控制弹出框
      formLabelWidth: "80px",
      isEditForm: false,
      typeForm: {
        uid: null,
        dictType: null,
        dictName: null,
        remark: null,
        isPublish: 1,
      },
      dataForm: {
        dictTypeUid:null,
        dictLabel:null,
        dictValue:null,
        listClass:"default",
        cssClass:null,
        remark:null,
        isDefault:null,
        isDefault: 0,
        isPublish: 1,
      },
      showHeader: true, //是否显示表头
      listClassType: [
        { key: 1, label: "default", value: "default" },
        { key: 2, label: "primary", value: "primary" },
        { key: 3, label: "success", value: "success" },
        { key: 4, label: "info", value: "info" },
        { key: 5, label: "warning", value: "warning" },
        { key: 6, label: "danger", value: "danger" },
      ],
      rules: {
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        dictLabel: [
          { required: true, message: "字典标签不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        dictValue: [
          { required: true, message: "字典键值不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        isDefault: [
          { required: true, message: "系统默认不能为空", trigger: "blur" },
        ],
        isPublish: [
          { required: true, message: "发布状态不能为空", trigger: "blur" },
        ]
      },
    };
  },
  created() {
    this.sysDictTypeList();
  },
  methods: {
    sysDictTypeList() {
      this.Loading = true;
      var params = {};
      params.keyword = this.keyword;
      params.currentPage = this.currentPage;
      params.pageSize = this.pageSize;
      getSysDictTypeList(params).then((response) => {
          this.tableData = response.data.records;
          this.currentPage = response.data.current;
          this.pageSize = response.data.size;
          this.total = response.data.total;
          this.Loading = false;
      });
    },
    handleFind() {
      this.sysDictTypeList();
    },
    handleAddSysDictType() {
      this.title = "增加字典类型";
      this.dialogTypeFormVisible = true;
      this.isEditForm = false;
    },
    handleEditSysDictType(row) {
      this.title = "编辑字典类型";
      this.isEditForm = true;
      Object.assign(this.typeForm, row);
      this.dialogTypeFormVisible = true;
    },
    submitTypeForm() {
      this.$refs.typeForm.validate((valid) => {
        if (valid) {
          if (this.isEditForm) {
            this.$store.dispatch('app/setLoading', true);
            editSysDictType(this.typeForm).then((response) => {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.handleTypeDialogClose();
                this.sysDictTypeList();
            });
          } else {
            this.$store.dispatch('app/setLoading', true);
            addSysDictType(this.typeForm).then((response) => {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.handleTypeDialogClose();
                this.sysDictTypeList();
            });
          }
        }
      });
    },
    handleDeleteSysDictType(row) {
      this.$confirm("此操作将把该字典类型删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let sysDictType = {};
          sysDictType.uid = row.uid;
          this.$store.dispatch('app/setLoading', true);
          deleteSysDictType(sysDictType).then((response) => {
            this.$message({
              type: "success",
              message: response.data,
            });
            this.sysDictTypeList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAddSysDictData(row) {
      this.title = "增加字典值";
      this.isEditForm = false;
      this.dataForm.dictTypeUid = row.uid;
      this.dialogDataFormVisible = true;
    },
    handleStickSysDictData(row){
      this.$confirm("此操作将会把该字典数据放到首位, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var params = {};
          params.uid = row.uid;
          this.$store.dispatch('app/setLoading', true);
          stickSysDictData(params).then((response) => {
              this.sysDictTypeList();
              this.$message({
                type: "success",
                message: response.data,
              });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消置顶",
          });
        });
    },
    handleEditSysDictData(row) {
      this.title = "编辑字典值";
      this.isEditForm = true;
      Object.assign(this.dataForm, row);
      this.dialogDataFormVisible = true;
    },
    handleDeleteSysDictData(row) {
      this.$confirm("此操作将把字典数据删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let sysDictData = {};
          sysDictData.uid = row.uid;
          this.$store.dispatch('app/setLoading', true);
          deleteSysDictData(sysDictData).then((response) => {
            this.$message({
              type: "success",
              message: response.data,
            });
            this.sysDictTypeList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //字典值新增/修改提交
    submitDataForm() {
      this.$refs.dataForm.validate((valid) => {
          if(valid) {
            if (this.isEditForm) {
              this.$store.dispatch('app/setLoading', true);
              editSysDictData(this.dataForm).then(response => {
                  this.$message({
                    type: "success",
                    message: response.data
                  });
                  this.handleDataDialogClose();
                  this.sysDictTypeList();
              });
            } else {
              this.$store.dispatch('app/setLoading', true);
              addSysDictData(this.dataForm).then(response => {
                  this.$message({
                    type: "success",
                    message: response.data
                  });
                  this.handleDataDialogClose();
                  this.sysDictTypeList();
              });
            }
          }
        })
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.sysDictTypeList();
    },
    handleTypeDialogClose() {
      this.typeForm = {
        uid: null,
        dictType: null,
        dictName: null,
        remark: null,
        isPublish: 1,
      };
      this.dialogTypeFormVisible = false;
    },
    handleDataDialogClose() {
      this.dataForm = {
        dictTypeUid:null,
        dictLabel:null,
        dictValue:null,
        listClass:"default",
        cssClass:null,
        remark:null,
        isDefault:null,
        isDefault: 0,
        isPublish: 1,
      };
      this.dialogDataFormVisible = false;
    },
  },
};
</script>

<style>
/* 二级表格对称 */
.el-table__expanded-cell[class*="cell"] {
  padding: 10px 0 10px 47.4px;
}
</style>